package com.zj.utils.view.dialog

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.zj.utils.R
import com.zj.utils.utils.http.NetConfig

class NetworkUtil {

    /**
     * 设置网络
     */
    fun showSetNetHttpDialog(context: Context) {
        val viewDialog = View.inflate(context, R.layout.layout_dialog_set_netip, null)
        viewDialog.measure(0, 0)
        val measuredHeight = viewDialog.measuredHeight
        val measuredWidth = viewDialog.measuredWidth
        val mDiaLog = LoginDiaLog(context, measuredWidth, measuredHeight, viewDialog, R.style.dialog)
        val IPSet = mDiaLog.findViewById<EditText>(R.id.IPSet)
        val HTTPSet = mDiaLog.findViewById<EditText>(R.id.HTTPSet)
        val setNetConfirm = mDiaLog.findViewById<Button>(R.id.SetNet_bt_confirm)
        val setNetCancel = mDiaLog.findViewById<Button>(R.id.SetNet_bt_cancel)
        val iptext = SPUtils.getInstance().getString("IPTEXT")
        val httptext = SPUtils.getInstance().getString("HTTPTEXT")
        IPSet.setText(iptext)
        HTTPSet.setText(httptext)
        setNetConfirm.setOnClickListener{
            val IPSetString = IPSet.text.toString().trim()
            val HTTPSetString = HTTPSet.text.toString().trim()
            if (!TextUtils.isEmpty(IPSetString) || !TextUtils.isEmpty(HTTPSetString)) {
                SPUtils.getInstance().put("IPTEXT", IPSetString)
                SPUtils.getInstance().put("HTTPTEXT", HTTPSetString)
                //http://192.168.32.98:8080/
                NetConfig.MAIN = "http://$IPSetString:$HTTPSetString"
                mDiaLog.dismiss()
            } else {
                ToastUtils.showShort("请填写完整")
            }
        }
        setNetCancel.setOnClickListener{ mDiaLog.dismiss() }
        mDiaLog.show()
    }


}
